<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
</head>

<body style="background-color: #FFFAE3">
<div class="x-nav" style="height: 43px">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
          </span>
    <i class="layui-icon layui-icon-refresh" style="line-height:35px"></i></a>
</div>
<div style="margin-top: 10px;margin-left: 270px;font-size: 18px;font-weight: bold;">
</div>
<table class="layui-hide" id="demo"  lay-filter="demo"></table>
<div id="formDiv" style="width: 500px;margin-left: 380px;#FFFAE3;padding: 30px;padding-left: 0px;bgColor:#FFFAE3" >
    <form class="layui-form" action="" id="fromOrd">
        <input type="hidden" id="orderId">
        <div class="layui-form-item"bgColor=#FFFAE3 borderColorLight=#FFFAE3>
            <label class="layui-form-label">物资编码</label>
            <div class="layui-input-block">
                <input id="materialCode" type="text" name="title" lay-verify="title" readonly="readonly"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物资名称</label>
            <div class="layui-input-block">
                <input id="materialName" type="text" name="title" lay-verify="title" readonly="readonly"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="text" id="amount" name="title" lay-verify="title" autocomplete="off" placeholder="请输入数量" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">计量单位</label>
            <div class="layui-input-block">
                <input id="measureUnit" type="text" name="title" lay-verify="title" readonly="readonly"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">预单价(元)</label>
            <div class="layui-input-block">
                <input type="text" id="unitPrice" name="title" lay-verify="title" autocomplete="off" placeholder="请输入预单价" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">交货期</label>
            <div class="layui-input-block">
                <input type="date" id="startDate" name="title" lay-verify="title" autocomplete="off" placeholder="请输入交货期" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" id="remark" name="title" lay-verify="title" autocomplete="off" placeholder="请输入备注" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="reset" onclick="chx()" class="layui-btn layui-btn-primary">重新选择物资</button>
                <button type="button" onclick="BaoCun()" class="layui-btn" lay-submit="" lay-filter="demo1">保存</button>
                <button type="button" onclick="BaoCunFll()" class="layui-btn" lay-submit="" lay-filter="demo1">全部需求录入完毕</button>
            </div>
        </div>
    </form>
</div>
</body>
<script id="toolbarDemo" type="text/html" style="background-color: #FFFAE3">
    <div className="layui-btn-container" style="width: 500px;margin-left: 500px;" id="divLeft">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">选择完毕</button>
    </div>
</script>
<script>
    let redioClick;
    layui.use('table', function () {
        $("#formDiv").hide();
        $("#span1").css("color","red"); //设置字体颜色为红色
        var table = layui.table;
        table.render({
            elem: '#demo'  //html中渲染table的id
            , url: '/Mater/'   //后台获取数据集合的地址
            , cellMinWidth: 80
            ,toolbar: '#toolbarDemo'
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false//不显示首页
                , last: false //不显示尾页

            }, cols: [
                [
                    //表单显示的title以及返回的需要显示的数据，field值与获取集合中的类的字段名一致。
                    {title: '选择', toolbar: "#checkbox",type:'checkbox', align: 'center'}
                    , {field: 'id', title: '序号', align: 'center'}
                    , {field: 'materialNum', title: '产品编码', align: 'center'}
                    , {field: 'materialName', title: '产品名称', align: 'center'}
                    , {field: 'orderId', title: 'id', align: 'center',hide:true}

                ]
            ]
        });
        //行单击事件（双击事件为：rowDouble）
        table.on('radio(demo)', function(obj){
            var listq=[];
            redioClick=obj.data;
            var data = obj.data;
            //
            // layer.alert(JSON.stringify(data), {
            //     title: '当前行数据：'
            // });
            // alert(data.orderId)
            $.ajax({
                url: "/orders/SelById",
                data:{"orderNum":data.orderId},
                type: "GET",
                success: function (result) {
                    $("#materialCode").val(result.materialCode);
                    $("#materialName").val(result.materialName);
                    $("#measureUnit").val(result.measureUnit);
                    $("#orderId").val(data.orderId);
                    $("#amount").val(result.amount);
                    if(result.unitPrice!=0){
                        $("#unitPrice").val(result.unitPrice);
                    }
                    $("#remark").val(result.remark);
                    $("#startDate").val(result.startDate);
                }
            });
            //标注选中样式
            //obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        });
        table.on('toolbar(demo)', function(obj){
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            if(checkStatus.data[0]!=null){
                $("#formDiv").show();
                $("#divLeft").hide();
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        let list=[];
                        for (let i = 0; i < data.length; i++) {
                            list.push(data[i].id)
                        }
                        listq=list;
                        $("#demo").attr("display","none");

                        table.reload('demo', {
                            url: '/Mater/SelListId',
                            method : 'GET',
                            where: {
                                'ids': list,
                                //,test: '新的 test2'
                                //,token: '新的 token2'
                            },
                            traditional: true
                            ,cols: [
                                [ // 重置表头
                                    {title: '选择', toolbar: "#checkbox",type:'radio', align: 'center'}
                                    , {field: 'id', title: '序号', align: 'center'}
                                    , {field: 'materialNum', title: '产品编码', align: 'center'}
                                    , {field: 'materialName', title: '产品名称', align: 'center'}
                                    , {field: 'materialUnit', title: '加量单位', align: 'center'}
                                    , {field: 'mstatic', title: '状态',align: 'center'}
                                    , {field: 'orderId', title: 'id', align: 'center',hide:true}
                                ]
                            ]
                        });

                        $("#span1").css("color","black");
                        $("#span2").css("color","red"); //设置字体颜色为红色
                        $("#divLeft").hide();
                        break;
                };
            }else{
                layer.confirm('请至少选择一个物资!', function(index){
                    layer.close(index);
                });
            }
        });
    });
    function BaoCunFll(){
        var datas=layui.table.cache["demo"];
        for (let data of datas) {
            if (data.mstatic == "未完成") {
                layer.alert("请录入全部需求!!!");
                break;
            }else{
                location.href='/planman/Order_ytb_list.html';
                break;
            }
        }
    }
    function BaoCun(){
        let orderId=$("#orderId").val();
        let amount=$.trim($("#amount").val());
        let unitPrice=$.trim($("#unitPrice").val());
        let startDate=$.trim($("#startDate").val());
        let remark=$.trim($("#remark").val());
        if(amount==null||unitPrice==null||startDate==null||amount==''||unitPrice==''||startDate==''){
            alert("未填写!")
        }else{
            $.ajax({
                url: "/orders/Update",
                data:{
                    "orderNum":orderId,
                    "amount":amount,
                    "unitPrice":unitPrice,
                    "startDate":startDate,
                    "remark":remark,
                },
                type: "POST",
                success: function (result) {
                    if($.trim(result)=="true"){
                        var datasid=[];
                        var datas=layui.table.cache["demo"];
                        for (let i = 0; i < datas.length; i++) {
                            if(datas[i].mstatic=="完成"){
                                datasid.push(datas[i].id)
                            }
                        }
                        let idds=[];
                        for (let i = 0; i < datas.length; i++) {
                            idds.push(datas[i].orderId)
                        }
                        var table = layui.table;
                        table.reload('demo',{
                            url:'/Mater/SelListId',
                            method:"POST",
                            where: {
                                'ids':listq,
                                'idd': redioClick.id,
                                'idds':idds,
                                'datasid':datasid
                                //,test: '新的 test2'
                                //,token: '新的 token2'
                            },
                        });
                    }
                    $(':input','#fromOrd')
                        .not(':button, :submit, :reset, :hidden')
                        .val('')
                        .removeAttr('checked')
                        .removeAttr('selected');
                    layer.alert("保存成功，请选择下一条要保存的数据！");
                }
            });
        }

    }
</script>
<script>
    function chx(){
        $.ajax({
            url: "/orders/DelWei",
            type: "GET",
            success: function (result) {
            }
        });
        location.href='/planman/pclass_select.html';
    }
</script>
</html>